package cn.sfturing.service.impl;

import cn.sfturing.dao.DoctoWorkMapper;
import cn.sfturing.dao.DoctorDao;
import cn.sfturing.dao.OrderRecordsDao;
import cn.sfturing.entity.DoctoWork;
import cn.sfturing.entity.Doctor;
import cn.sfturing.entity.OrderRecords;
import cn.sfturing.service.DoctoWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class DoctoWorkServiceImpl implements DoctoWorkService {

    @Autowired
    DoctoWorkMapper doctoWorkMapper;
    @Autowired
    OrderRecordsDao orderRecordsDao;
    @Autowired
    DoctorDao doctorDao;

    @Override
    public List<DoctoWork> selecrByIdAndDate(Integer doctorId, String startDate, String endDate) {
        return doctoWorkMapper.selecrByIdAndDate(doctorId,startDate,endDate);
    }
    @Override
    public boolean updateByIdAndDate(DoctoWork doctoWork) {
        int i = doctoWorkMapper.updateByIdAndDate(doctoWork);
        return i>0?true:false;
    }
    @Override
    public boolean insertSelective(DoctoWork doctoWork) {
        int i = doctoWorkMapper.insertSelective(doctoWork);
        return i>0?true:false;
    }

    @Override
    public DoctoWork selecrByIdAndDateOne(int doctorId, String date) {
        return doctoWorkMapper.selecrByIdAndDateOne(doctorId,date);
    }

    @Override
    public boolean addDoctoWork(Integer id) {
        boolean tf=false;
        OrderRecords orderRecords1 = orderRecordsDao.findOrderById(id);//查找订单信息
        Doctor doctor = doctorDao.findDoctorByName(orderRecords1.getDoctorName(),orderRecords1.getOfficesName(),orderRecords1.getHospitalName());//跟据医院名、科室名、医生名字得到医生
        DoctoWork doctoWork = doctoWorkMapper.selecrByIdAndDateOne(doctor.getId(), orderRecords1.getTransactDate());
        if(doctoWork==null){//判断时段信息是否存在
            DoctoWork doctoWork1 = new DoctoWork();
            doctoWork1.setDoctorId(doctor.getId());
            if(orderRecords1.getTransactTime().equals("8:00-11:00")){
                doctoWork1.setTimeS("1");doctoWork1.setTimeZ("0");doctoWork1.setTimeX("0");
            }else if(orderRecords1.getTransactTime().equals("13:00-15:00")){
                doctoWork1.setTimeS("0");doctoWork1.setTimeZ("1");doctoWork1.setTimeX("0");
            }else {
                doctoWork1.setTimeS("0");doctoWork1.setTimeZ("0");doctoWork1.setTimeX("1");
            }
            doctoWork1.setWorkDate(orderRecords1.getTransactDate());
            doctoWork1.setGmtCreate(new Date());
            doctoWork1.setGmtModified(new Date());
            doctoWorkMapper.insertSelective(doctoWork1);
            tf=true;
        }else{
            if(orderRecords1.getTransactTime().equals("8:00-11:00")){
                doctoWork.setTimeS("1");
            }else if(orderRecords1.getTransactTime().equals("13:00-15:00")){
                doctoWork.setTimeZ("1");
            }else {
                doctoWork.setTimeX("1");
            }
            doctoWork.setGmtModified(new Date());
            doctoWorkMapper.updateByIdAndDate(doctoWork);
            tf=true;
        }
        return tf;
    }
}
